<script setup lang="ts">
import { delPatientAPI, editPatientAPI, getPatientListAPI } from '@/services/user'
import { computed, onMounted, ref, watch } from 'vue'
import { type AddPatient, type Patient } from '@/types/user'
import { showConfirmDialog, showSuccessToast, showToast } from 'vant'
import Validator from 'id-validator'
import { addPatientAPI } from '@/services/user'

const list = ref<Patient[]>([])
const loadData = async () => {
  const res = await getPatientListAPI()
  list.value = res.data
}
onMounted(() => {
  loadData()
})
// 显示新增弹窗
const isShow = ref(false)

const options = [
  { label: '男', value: 1 },
  { label: '女', value: 0 }
]

const initFormData: AddPatient = {
  name: '测试2',
  idCard: '110101196501023433',
  gender: 1,
  defaultFlag: 0
}

// 2. 声明表单初始值，数据数据双向绑定
const formData = ref<AddPatient>({ ...initFormData })
// 默认值需要转换
const defaultFlag = computed({
  get() {
    return formData.value.defaultFlag === 1
  },
  set(value) {
    formData.value.defaultFlag = value ? 1 : 0
  }
})
// 4. 关闭弹出层时，还原表单初始值
watch(isShow, (newValue) => {
  if (!newValue) {
    // 还原表单的初始值
    formData.value = { ...initFormData }
  }
})
const submit = async () => {
  if (!formData.value.name) return showToast('请输入真实姓名')
  if (!formData.value.idCard) return showToast('请输入身份证号')
  const validator = new Validator()
  if (!validator.isValid(formData.value.idCard)) return showToast('请输入正确的身份证号')
  formData.value.id ? await editPatientAPI(formData.value) : await addPatientAPI(formData.value)
  showSuccessToast(formData.value.id ? '编辑成功' : '添加成功')
  loadData()
  isShow.value = false
}

const showEdit = (item: Patient) => {
  isShow.value = true
  const { id, gender, name, idCard, defaultFlag } = item
  formData.value = { id, gender, name, idCard, defaultFlag }
}
const delById = async () => {
  await showConfirmDialog({
    title: '温馨提示',
    message: `您确认要删除 ${formData.value.name} 患者信息吗 ？`,
    cancelButtonText: '取消',
    confirmButtonText: '确认'
  })
  await delPatientAPI(formData.value.id as string)
  isShow.value = false
  loadData()
  showSuccessToast('删除成功')
}
</script>

<template>
  <div class="patient-page">
    <cp-nav-bar title="家庭档案" />
    <div class="patient-list">
      <div class="patient-item" v-for="item in list" :key="item.id">
        <div class="info">
          <span class="name">{{ item.name }}</span>
          <span class="id">{{ item.idCard.replace(/^(\d{6})\d{8}(\d{4})$/, '\$1******\$2') }}</span>
          <span>{{ item.genderValue }}</span>
          <span>{{ item.age }}岁</span>
        </div>
        <div @click="showEdit(item)" class="icon"><cp-icon name="user-edit" /></div>
        <div class="tag" v-if="item.defaultFlag === 1">默认</div>
      </div>
      <div class="patient-add" v-if="list.length < 6"></div>

      <div class="patient-add" @click="isShow = true">
        <cp-icon name="user-add" />
        <p>添加患者</p>
      </div>
      <div class="patient-tip">最多可添加 6 人</div>
    </div>
    <!--新增 修改 删除-->
    <van-popup v-model:show="isShow" position="right">
      <cp-nav-bar
        :title="formData.id ? '编辑患者' : '添加患者'"
        right-text="保存"
        :on-back="() => (isShow = false)"
        @click-right="submit"
      />
      <van-form autocomplete="off">
        <van-action-bar v-if="formData.id">
          <van-action-bar-button @click="delById">删除</van-action-bar-button>
        </van-action-bar>
        <van-field v-model="formData.name" label="真实姓名" placeholder="请输入真实姓名" />
        <van-field v-model="formData.idCard" label="身份证号" placeholder="请输入身份证号" />
        <van-field label="性别">
          <!-- 单选按钮组件 -->
          <template #input>
            <cp-radio-btn :options="options" v-model="formData.gender"></cp-radio-btn>
          </template>
        </van-field>
        <van-field label="默认就诊人">
          <template #input>
            <van-checkbox round v-model="defaultFlag" />
          </template>
        </van-field>
      </van-form>
    </van-popup>
  </div>
</template>

<style lang="scss" scoped>
.patient-page {
  padding: 0 0 80px;
  ::v-deep() {
    .van-popup {
      width: 100%;
      height: 100%;
    }
  }
}
.patient-list {
  padding: 15px;
}
.patient-item {
  display: flex;
  align-items: center;
  padding: 15px;
  background-color: var(--cp-bg);
  border-radius: 8px;
  margin-bottom: 15px;
  position: relative;
  border: 1px solid var(--cp-bg);
  transition: all 0.3s;
  overflow: hidden;
  .info {
    display: flex;
    flex-wrap: wrap;
    flex: 1;
    span {
      color: var(--cp-tip);
      margin-right: 20px;
      line-height: 30px;
      &.name {
        font-size: 16px;
        color: var(--cp-text1);
        width: 80px;
        margin-right: 0;
      }
      &.id {
        color: var(--cp-text2);
        width: 180px;
      }
    }
  }
  .icon {
    color: var(--cp-tag);
    width: 20px;
    text-align: center;
  }
  .tag {
    position: absolute;
    right: 60px;
    top: 21px;
    width: 30px;
    height: 16px;
    font-size: 10px;
    color: #fff;
    background-color: var(--cp-primary);
    border-radius: 2px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  &.selected {
    border-color: var(--cp-primary);
    background-color: var(--cp-plain);
    .icon {
      color: var(--cp-primary);
    }
  }
}
.patient-add {
  background-color: var(--cp-bg);
  color: var(--cp-primary);
  text-align: center;
  padding: 15px 0;
  border-radius: 8px;
  .cp-icon {
    font-size: 24px;
  }
}
.patient-tip {
  color: var(--cp-tag);
  padding: 12px 0;
}
.van-action-bar {
  padding: 0 10px;
  margin-bottom: 10px;
  .van-button {
    color: var(--cp-price);
    background-color: var(--cp-bg);
  }
}
</style>
